



//
//  NPersonalFillDataVC.m
//  nncloud
//
//  Created by macfai on 2018/12/19.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NPersonalFillDataVC.h"
#import "NDataCell.h"
#import "NMineViewController.h"
@interface NPersonalFillDataVC ()<UITableViewDelegate,UITableViewDataSource,SHAddImgViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSArray *titles;

//营业执照存放数组
@property (nonatomic,strong)NSArray *photos;

//营业执照附件存放数组
@property (nonatomic,strong)NSArray *photosCopy;

///企业名称
@property (nonatomic,copy)NSString *enterPriseName;

@property (nonatomic,strong)NSMutableArray *zhizhaoArray;

@property (nonatomic,copy)NSString *enterPriseCode;
//@property (nonatomic,copy)NSString *contactName;
@property (nonatomic,copy)NSString *tel;

@property (nonatomic,assign)NSInteger cellIndex;


@property (nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation NPersonalFillDataVC

#define cellIdentifier @"datacell"
-(UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NDataCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [NGlobalModel shareInstance].isAuth = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [NGlobalModel shareInstance].isAuth = NO;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"资料填写" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.zhizhaoArray = [NSMutableArray array];
    self.cellIndex = 0;
    self.dataArray = [NSMutableArray array];
    self.titles = @[@"姓名",@"身份证号",@"联系人电话",@"",@"",@""];
    [self.view addSubview:self.tableView];
    [self addFooter];
    //    [self loadData];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)addFooter {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100.f)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.uxy_acceptEventInterval = 3.f;
    btn.frame = CGRectMake(0, 30, ScreenWidth-60, 50);
    btn.centerX = footer.centerX;
    btn.backgroundColor = ThemeBlue;
    btn.layer.cornerRadius = 5.f;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"提交" forState:UIControlStateNormal];
    [btn addTarget: self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
    self.tableView.tableFooterView = footer;
}



 
#pragma mark - 认证提交
- (void)tapAction:(UIButton *)btn
{
    //认证状态  登录后返回字段  userInfoCertificationStatus  0审核中  1已认证  2未通过   3未认证
    //identType  认证类型  0个人  1企业
    //个人端就是个人的名字
    if (self.enterPriseName.length==0||self.tel.length==0||self.enterPriseCode.length==0) {
        [AFToast showText:@"请填写认证资料"];
        return;
    }
    
    NSDictionary *dic =  @{@"identId":[NUserModel shareInstance].userInfoId?:@"1",@"identType":[NUserModel shareInstance].userInfoType,@"identEnterpriseName":self.enterPriseName,@"identEnterpriseCode":self.enterPriseCode,@"identTelephone":self.tel};
    //图片资源不提交也可以成功
    [NNetworkClient uploadImage:self.zhizhaoArray url:kAccoutAuthUrl parameters:dic success:^(id responseObject) {
        YYLog(@"%@",responseObject);
        WeakStament(ws);
        if ([responseObject[@"code"] integerValue]==0) {
            [AFToast showText:@"认证提交成功,请等待审核结果"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                //跳回到指定页面  跳转指定页面
                //NSArray * ctrlArray = self.navigationController.viewControllers;
                //[ws.navigationController popToViewController:ctrlArray[1] animated:YES];
                [ws.navigationController popToRootViewControllerAnimated:YES];
 
            });
        }
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        
    } delegater:nil];
    
}


#pragma mark - delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row>2) {
        return 130.f;
    }else{
        return 60.f;
    }
    return 0;
}


#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NDataCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.titleLabl.text = self.titles[indexPath.row];
    
    if (indexPath.row>2) {
        cell.dataTF.hidden = YES;
    }
    if (indexPath.row==3||indexPath.row==4||indexPath.row==5) {
        [self setUIWithCell:cell withIndexPath:indexPath];
    }
    cell.dataTF.tag = indexPath.row+220;
    cell.dataTF.delegate = self;
    //字数实时监听
    [cell.dataTF addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];

    //底部线条
//    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(5,indexPath.row>2?129:59, ScreenWidth-10, 0.5)];
//    line.backgroundColor = kGrayColor;
//    [cell addSubview:line];
    return cell;
}




///设置cell 内容
- (void)setUIWithCell:(NDataCell *)cell withIndexPath:(NSIndexPath *)indexPath{
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(23,15,200,18)];
    if (indexPath.row==3) {
        
        label.text =@"身份证原件正面";
        self.cellIndex = 3;
        
    }else if (indexPath.row==4){
        
        label.text =@"身份证原件背面";
        self.cellIndex = 4;
    }else{
        label.text =@"手持身份证半身照";
        self.cellIndex = 5;
    }
    label.textColor = kDarkGrayColor;
    label.font = kFont15;
    [cell.contentView addSubview:label];
    [self addPhotoAreaOnCell:cell];
    
}

///添加照片选择区域
- (void)addPhotoAreaOnCell:(NDataCell *)cell{
    WeakStament(ws);
    if (self.cellIndex==3) {
        SHAddImgView *addImgView = [[SHAddImgView alloc] initWithMaxColumn:4];
        addImgView.frame = CGRectMake(22, 33, ScreenWidth, 95);
        addImgView.configModel = [SHConfigModel creatNormalConfig];
        addImgView.backgroundColor = kWhiteColor;
        addImgView.delegate = self;
        addImgView.ImgBlock = ^(NSArray *photos) {
            YYLog(@"%@",photos);
            ws.photos = photos;
            [ws.zhizhaoArray addObject:[photos lastObject]];
        };
        [cell.contentView addSubview:addImgView];
    }else if (self.cellIndex==4){
        SHAddImgView *addImgView = [[SHAddImgView alloc] initWithMaxColumn:4];
        addImgView.frame = CGRectMake(22, 33, ScreenWidth, 95);
        addImgView.configModel = [SHConfigModel creatNormalConfig];
        addImgView.backgroundColor = kWhiteColor;
        addImgView.delegate = self;
        addImgView.ImgBlock = ^(NSArray *photos) {
            YYLog(@"%@",photos);
            self.photos = photos;
            [ws.zhizhaoArray addObject:[photos lastObject]];
        };
        //    _addImgView = addImgView;
        [cell.contentView addSubview:addImgView];
    }else{
        SHAddImgView *addImgView = [[SHAddImgView alloc] initWithMaxColumn:4];
        addImgView.frame = CGRectMake(22, 33, ScreenWidth, 95);
        addImgView.configModel = [SHConfigModel creatNormalConfig];
        addImgView.backgroundColor = kWhiteColor;
        addImgView.delegate = self;
        addImgView.ImgBlock = ^(NSArray *photos) {
            YYLog(@"%@",photos);
            self.photos = photos;
            [ws.zhizhaoArray addObject:[photos lastObject]];
        };
        //    _addImgView = addImgView;
        [cell.contentView addSubview:addImgView];
    }
    
}

///获取cell的textField输入的内容
- (void)textFieldWithText:(UITextField *)tf{
    
    switch (tf.tag) {
        case 220:
        {
            //名字
            self.enterPriseName = tf.text;
            [NGlobalModel shareInstance].personName = self.enterPriseName;
            [self restrictWordNumber:tf];
            YYLog(@"%@",tf.text);
        }
            break;
        case 221:
        {
            self.enterPriseCode = tf.text;
            
            [NGlobalModel shareInstance].personIdCard = self.enterPriseCode;
            [self restrictWordNumber:tf];
            YYLog(@"%@",tf.text);
        }
            break;
        case 222:
        {
            
        //这个页面是个人认证   这里是联系人电话
            self.tel = tf.text;
            [NGlobalModel shareInstance].personTel = self.tel;
            [self restrictWordNumber:tf];
            YYLog(@"%@",tf.text);
        }
            break;
//        case 203:
//        {
//            self.tel = tf.text;
//
//            [NGlobalModel shareInstance].tel = self.tel;
//            [self restrictWordNumber:tf];
//            YYLog(@"%@",tf.text);
//        }
//            break;
        default:
            break;
    }
}

- (void)restrictWordNumber:(UITextField *)textField{
    
    if (textField.tag==220) {
        
        if (textField.text.length > 10) {
            UITextRange *markedRange = [textField markedTextRange];
            if (markedRange) {
                return;
            }
            //Emoji占2个字符，如果是超出了半个Emoji，用15位置来截取会出现Emoji截为2半
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:10];
            textField.text = [textField.text substringToIndex:range.location];
        }
    }else if (textField.tag==221){
        
        ///在didEnd代理中进行正则判断
        
//        if (textField.text.length > 100) {
//            UITextRange *markedRange = [textField markedTextRange];
//            if (markedRange) {
//                return;
//            }
//            NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:100];
//            textField.text = [textField.text substringToIndex:range.location];
//        }
    }else if (textField.tag==222){
        
        ///在didEnd代理中进行正则判断  有些时候可以结合着一起使用  
//        if (textField.text.length > 10) {
//            UITextRange *markedRange = [textField markedTextRange];
//            if (markedRange) {
//                return;
//            }
//            NSRange range = [textField.text rangeOfComposedCharacterSequenceAtIndex:10];
//            textField.text = [textField.text substringToIndex:range.location];
//        }
        
    }
    
}

#pragma mark - textField delegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag==221) {
        ///身份证号
        if (![ToolKit validateIdCard:textField.text]) {
            [AFToast showText:@"请输入正确的身份证号"];
            textField.text = @"";
            return;
        }
    }else if (textField.tag==222){
        ///电话
        if (![ToolKit validateMobile:textField.text]) {
            [AFToast showText:@"请输入正确的电话号码"];
            textField.text = @"";
            return;
        }
    }
}

@end
